﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace PMS.Controllers.BD
{
    public class PermissionController : Controller
    {
        //
        // GET: /Permission/
        public ActionResult PermissionList()
        {
            return View();
        }
        public ActionResult List(View_BD_Permission permission)
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            listSearch.Add(new SearchCondition() { Field="Parent",Operator="!=",Value="0"});
            Expression<Func<View_BD_Permission, bool>> exp = SearchExpression.FindByGroup<View_BD_Permission>(permission, listSearch);
            string OrderId = OperateContext.Sort == null ? "Id" : OperateContext.Sort;
            int count = 0;
            var list = BLLSession.Current.View_BD_PermissionBLL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, o => o.Id, true);
            count = BLLSession.Current.View_BD_PermissionBLL.GetCount(exp);
            return Json(new EasyuiDataPager() { rows = list, total = count });
        }

        public ActionResult EditPermission(BD_Permission permission)
        {
            permission.AddTime = DateTime.Now.ToString("yyyy-MM-dd");
            permission.Clerk = OperateContext.sessUserInfo.Name;
            BLLSession.Current.BD_PermissionBLL.ModifyBy(permission, p => p.Id == permission.Id, "Parent", "Name", "OrderField", "LinkUrl", "Note", "AddTime", "IsShow", "Clerk", "IsShortCut", "ShortCutPic", "IsFatherNode");
            return Json(new Message() { msg = "保存成功", state = "success" });
        }
        public ActionResult DelPermission(int Id)
        {
            BLLSession.Current.BD_PermissionBLL.DelBy(d => d.Id == Id);
            return Json(new Message() { msg = "保存成功", state = "success" });
        }
        public ActionResult AddPermission(BD_Permission permission)
        {
            permission.AddTime = DateTime.Now.ToString("yyyy-MM-dd");
            permission.Clerk = OperateContext.sessUserInfo.Name;
            BLLSession.Current.BD_PermissionBLL.Add(permission);
            return Json(new Message() { msg = "保存成功", state = "success" });
        }
        public ActionResult GetUserBtnPermission(string url)
        {
            List<BD_btnPermission> btnPerm = new List<BD_btnPermission>();
            string shortUrl = url.Split('?')[0];
            BD_Permission perm = OperateContext.SessPermission.Where(p => p.LinkUrl == shortUrl).FirstOrDefault();
            if (perm != null)
            {
                btnPerm = OperateContext.SessbtnPermission.Where(b => b.perId == perm.Id).OrderBy(b => b.Order).ToList();
            }
            return Json(btnPerm);
        }

        public JsonResult GetParentPerm()
        {
            var list=DALSession.Current.BD_PermissionDAL.GetListBy(p => p.IsFatherNode == "0").Select(p => new { value = p.Id, text = p.Name });
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetSubPerm()
        {
            var list = DALSession.Current.BD_PermissionDAL.GetListBy(p => p.IsFatherNode == "1").Select(p => new { value = p.Id, text = p.Name });
            return Json(list, JsonRequestBehavior.AllowGet);
        }
    }
}
